package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.mapper.GatherMapper;
import com.guigu.youxuan.pojo.*;
import com.guigu.youxuan.pojo.dto.GatherDto;
import com.guigu.youxuan.service.IGatherService;
import com.guigu.youxuan.service.IPurchaseOrderService;
import com.guigu.youxuan.service.IWarehouseDetailsService;
import com.guigu.youxuan.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.youxuan.mapper.GatherMapper;
import com.guigu.youxuan.pojo.Gather;
import com.guigu.youxuan.pojo.GatherDetails;
import com.guigu.youxuan.pojo.PurchaseOrder;
import com.guigu.youxuan.pojo.PurchaseOrderDetails;
import com.guigu.youxuan.pojo.dto.GatherMakeDto;
import com.guigu.youxuan.pojo.dto.PurchaseAndDetailDto;
import com.guigu.youxuan.service.IGatherDetailsService;
import com.guigu.youxuan.service.IGatherService;
import com.guigu.youxuan.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class GatherServiceImpl extends ServiceImpl<GatherMapper, Gather> implements IGatherService {
    @Autowired
    private IWarehouseDetailsService iWarehouseDetailsService;
    @Autowired
    private IPurchaseOrderService iPurchaseOrderService;
    @Autowired
    private IWarehouseService iWarehouseService;
    @Autowired
    private GatherMapper gatherMapper;

    @Autowired
    private IGatherDetailsService gatherDetailsService;

    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    /**
     * 入库审核查询状态为S001-1待审核的信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Gather> InStorageFuhePage(int currentPage, int pageSize) {
        QueryWrapper<Gather> wrapper=new QueryWrapper<>();
        wrapper.eq("check_tag","S001-1");
        PageHelper.startPage(currentPage,pageSize);
        List<Gather> goodsInfos=this.list(wrapper);
        PageInfo<Gather> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    /**
     * 仓库列表查询所有仓库信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Gather> InStorageListPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Gather> goodsInfos=this.list();
        PageInfo<Gather> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    /**
     * 入库审核通过
     * @param gatherDto
     */
    @Transactional
    @Override
    public void YesFuheReviewList(GatherDto gatherDto) {
        //采购订单表拿入库表的采购id
        PurchaseOrder byId = iPurchaseOrderService.getById(gatherDto.getGather().getPurchaseId());
        //更改他的两个状态
        byId.setGatherTag("K001-2");
        byId.setConveyTag("P001-5");
        iPurchaseOrderService.updateById(byId);

        //第一个对象用dto里的gather
        Gather gather =gatherDto.getGather();
        //给审核时间设置时间
        gather.setCheckTime(new Date());
        //给复核标志改为 审核通过
        gather.setCheckTag("S001-3");
        this.updateById(gather);



        //第二个对象用dto里的gatherDetails
        List<GatherDetails> list=gatherDto.getGatherDetails();
        //for循环赋值给gatherDetails
        for (GatherDetails gatherDetail :list){
              QueryWrapper<WarehouseDetails> wrapper=new QueryWrapper<>();
              //查询条件
              wrapper.eq("warehouse_id",gatherDetail.getWarehouseId());
              wrapper.eq("goods_id",gatherDetail.getGoodsId());
              wrapper.eq("supplier_id",gatherDetail.getSupplierId());
              WarehouseDetails one = iWarehouseDetailsService.getOne(wrapper);
              //判断他是否为空
              if (one != null){
                  //如果有这条数据就把入库明细表里的数据修改 另外加上当前库存数量
                  one.setNowAmount(one.getNowAmount()+gatherDetail.getAmount());
                    iWarehouseDetailsService.updateById(one);

              }else {
                  //如果没有查询到相应的数据就新增一条数据把入库明细表数据赋值给仓库商品表
                  WarehouseDetails warehouseDetails=new WarehouseDetails();
                    warehouseDetails.setGoodsId(gatherDetail.getGoodsId());
                    warehouseDetails.setWarehouseId(gatherDetail.getWarehouseId());
                    warehouseDetails.setSupplierId(gatherDetail.getSupplierId());
                    warehouseDetails.setNowAmount(gatherDetail.getAmount());
                    iWarehouseDetailsService.save(warehouseDetails);
              }
            //new仓库对象 用仓库service.getByid查入库表里面的仓库id
            Warehouse warehouse = iWarehouseService.getById(gatherDetail.getWarehouseId());
            warehouse.setNowAmount(0);
            //设置仓库的当前存储量为入库表的总数量
            warehouse.setNowAmount(gather.getAmountSum()+warehouse.getNowAmount());
            //仓库service修改当前存储量
            iWarehouseService.updateById(warehouse);
        }
    }

    @Override
    public String getMaxDesignId() {
        Gather gather = gatherMapper.getMaxDesignId();
        if(gather == null){
            return null;
        }
        // 获取设计单编号
        String designId = gather.getGatherOrderNumber();
        return designId;
    }

    @Transactional
    @Override
    public void gatherMake(Gather gather, List<PurchaseOrderDetails> purchaseOrderDetailsList,Integer supplierId) {
        this.save(gather);
        for(PurchaseOrderDetails p : purchaseOrderDetailsList) {
            GatherDetails gatherDetails = new GatherDetails();
            gatherDetails.setGatherId(gather.getGatherId());
            gatherDetails.setGoodsId(p.getGoodsId());
            gatherDetails.setSupplierId(supplierId);
            gatherDetails.setWarehouseId(p.getWarehouseId());
            gatherDetails.setAmount(p.getCount());
            gatherDetails.setCostPrice(p.getPrice());
            gatherDetails.setSubtotal(p.getSubtotal());
            gatherDetailsService.save(gatherDetails);
        }

    }
}
